package hud;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class ConnectionTab extends Tab {
    private static ConnectionTab instance = null;
    private TextField nameTextField;
    private TextField hostTextField;
    private TextField portField;

    private ConnectionTab() {

    }

    public static ConnectionTab Instance() {
        if (instance == null) {
            instance = new ConnectionTab();
        }
        return instance;
    }

    public Tab construct() {
        Tab tab = new Tab("Connections");
        tab.setClosable(false);

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(0,0,20,0));
        VBox topVBox = new VBox();
        topVBox.setPadding(new Insets(0, 0, 40, 0));
        VBox bottomVBox = new VBox();
        vBox.getChildren().addAll(topVBox, bottomVBox);

        final ComboBox typeComboBox = new ComboBox();
        typeComboBox.getItems().addAll(
                "Direct Connection",
                "Repleca Set"
        );

        ColumnConstraints column1 = new ColumnConstraints(100,100, Double.MAX_VALUE);
        ColumnConstraints column2 = new ColumnConstraints(100,100, Double.MAX_VALUE);
        column2.setHgrow(Priority.ALWAYS);
        GridPane grid = new GridPane();
        grid.getColumnConstraints().addAll(column1, column2);
        grid.setVgap(8);
        grid.setHgap(4);
        grid.setPadding(new Insets(5, 5, 5, 5));

        // inside grid: row 1
        grid.add(new Label("To: "), 0, 0);
        grid.add(typeComboBox, 1, 0);
        typeComboBox.setMinWidth(250);
        typeComboBox.setMaxWidth(450);
        typeComboBox.setPrefWidth(450);
        typeComboBox.getSelectionModel().select(0);

        // inside grid: row 2
        grid.add(new Label("Name: "), 0, 1);
        nameTextField = new TextField("New Connection");
        grid.add(nameTextField, 1, 1);

        // inside grid: row 3
        grid.add(new Label("Address: "), 0, 2);
        HBox dbHostBox = new HBox();
        hostTextField = new TextField("localhost");
        hostTextField.setMinWidth(380);
        Label commaLabel = new Label(":");
        commaLabel.setMinWidth(20);
        commaLabel.setAlignment(Pos.BASELINE_CENTER);
        portField = new TextField("27017");
        portField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (!newValue.matches("\\d{0,7}?")) {
                    portField.setText(oldValue);
                }
            }
        });
        dbHostBox.getChildren().addAll(hostTextField, commaLabel, portField);
        grid.add(dbHostBox, 1, 2);

        // inside grid: row 4
        grid.add(new Label(""), 0, 3);
        Label descriptionLabel = new Label("Specify host and port of MongoDB Server. Host can be either IPv4, IPv6 or domain name.");
        descriptionLabel.setWrapText(true);
        descriptionLabel.setMaxHeight(30);
        grid.add(descriptionLabel, 1, 3);
        topVBox.getChildren().add(grid);

        Separator separator = new Separator();
        separator.setPrefWidth(500);
        HBox fromSRVHBox = new HBox();
        fromSRVHBox.setPadding(new Insets(10, 10, 0, 10));
        fromSRVHBox.setSpacing(20);
        Button fromSRVButton = new Button("From SRV");
        TextField fromSRVTextField = new TextField("Import connection details from MongoDB SRV connection");
        fromSRVTextField.setMinWidth(450);
        fromSRVHBox.getChildren().addAll(fromSRVButton, fromSRVTextField);
        bottomVBox.getChildren().addAll(separator, fromSRVHBox);
        tab.setContent(vBox);
        return tab;
    }

    public String getName() {
        return nameTextField.getText();
    }

    public String getHost() {
        return hostTextField.getText();
    }

    public String getPort() {
        return portField.getText();
    }
}
